﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Stone
{
    public partial class FormMain : Form
    {
        //Gps
        public GPSHandler GpsHandler;

        //Variables
        int maxNMEAsentecesInListBox = 20;
        bool recordInProgress = false;

        public FormMain()
        {
            InitializeComponent();
            Settings.LoadSettings();
            GpsHandler = new GPSHandler(this);
            GpsHandler.NewGPSFix += new GPSHandler.NewGPSFixHandler(this.GPSEventHandler);
        }
        private void GPSEventHandler(object sender, GPSHandler.GPSEventArgs e)
        {
            ReceiveNMEA(GpsHandler, e);
        }
        public void ReceiveNMEA(GPSHandler GpsHandler_p, GPSHandler.GPSEventArgs e)
        {            
            switch (e.TypeOfEvent)
            {
                case GPSEventType.GPRMC:  //Recommended minimum specific GPS/Transit data
                    addNMEAsentenceInListBox(GpsHandler_p.GPRMC.NMEA_string);
                    if (recordInProgress) File.AppendAllText(Settings.NMEA_tmp_logging_path, DateTime.Now.ToString() + GpsHandler_p.GPRMC.NMEA_string + "\n"); 
                    break;

                case GPSEventType.GPGSV:
                    addNMEAsentenceInListBox(GpsHandler_p.GPGSV.NMEA_string);
                    if (recordInProgress) File.AppendAllText(Settings.NMEA_tmp_logging_path, DateTime.Now.ToString() + GpsHandler_p.GPGSV.NMEA_string + "\n"); 
                    break;

                case GPSEventType.PGRME:
                    addNMEAsentenceInListBox(GpsHandler_p.PGRME.NMEA_string);
                    if (recordInProgress) File.AppendAllText(Settings.NMEA_tmp_logging_path, DateTime.Now.ToString() + GpsHandler_p.PGRME.NMEA_string + "\n"); 
                    break;

                case GPSEventType.GPGSA:
                    addNMEAsentenceInListBox(GpsHandler_p.GPGSA.NMEA_string);
                    if (recordInProgress) File.AppendAllText(Settings.NMEA_tmp_logging_path, DateTime.Now.ToString() + GpsHandler_p.GPGSA.NMEA_string + "\n"); 
                    break;

                case GPSEventType.GPGLL:
                    addNMEAsentenceInListBox(GpsHandler_p.GPGLL.NMEA_string);
                    if (recordInProgress) File.AppendAllText(Settings.NMEA_tmp_logging_path, DateTime.Now.ToString() + GpsHandler_p.GPGLL.NMEA_string + "\n"); 
                    break;

                case GPSEventType.GPGGA:
                    addNMEAsentenceInListBox(GpsHandler_p.GPGGA.NMEA_string);
                    if (recordInProgress) File.AppendAllText(Settings.NMEA_tmp_logging_path, DateTime.Now.ToString() + GpsHandler_p.GPGGA.NMEA_string + "\n"); 
                    break;
            }
        }
        private void addNMEAsentenceInListBox(string NMEAsentence)
        {
            if (listBox_LOG.Items.Count >= maxNMEAsentecesInListBox)
                listBox_LOG.Items.RemoveAt(0);

            listBox_LOG.Items.Add(NMEAsentence);
        }
        private void button_Start_logging_Click(object sender, EventArgs e)
        { 
            GpsHandler.Start(Settings.GPS_ComPort, int.Parse(Settings.GPS_ComPort_Baudrate));
            button_Start_logging.Enabled = false;
            button_Stop_logging.Enabled = true;
            button_Stop_Recording.Enabled = false;
            button_Start_Recording.Enabled = true;
        }
        private void button_Stop_logging_Click(object sender, EventArgs e)
        {
            GpsHandler.Stop();
            button_Stop_logging.Enabled = false;
            button_Start_logging.Enabled = true;
            button_Stop_Recording.Enabled = false;
            button_Start_Recording.Enabled = false;
        }
        private void button_Start_Recording_Click(object sender, EventArgs e)
        {
            File.Delete(Settings.NMEA_tmp_logging_path);
            recordInProgress = true;
            button_Stop_Recording.Enabled = true;
            button_Start_Recording.Enabled = false;
            button_Stop_logging.Enabled = false;
        }
        private void button_Stop_Recording_Click(object sender, EventArgs e)
        {
            recordInProgress = false;

            if (saveFileDialog_Recording.ShowDialog() == DialogResult.OK)
            {
                File.Copy(Settings.NMEA_tmp_logging_path, saveFileDialog_Recording.FileName);
            }

            button_Start_Recording.Enabled = true;
            button_Stop_Recording.Enabled = false;
            button_Stop_logging.Enabled = true;
        }
    }
}
